﻿// Inner Fire 游戏引擎库
// FrameResourceBuffers 帧资源缓冲列表
//
// Copyright (c) 2024 by 尔西大帝. All rights reserved.
//
// 用于管理动态创建的BUFFER
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2024-11-19

#pragma once

#include <EASTL/vector.h>
#include <dx/core/upload_buffer.h>
#include <memory>

namespace ifire::dx {
class FrameResource;

template <typename T> class FrameResourceBuffers {
public:
  FrameResourceBuffers(FrameResource* owner, bool is_constant_buffer)
      : owner_(owner), is_constant_(is_constant_buffer) {}
  FrameResourceBuffers(const FrameResourceBuffers& lhs) = delete;
  FrameResourceBuffers& operator=(const FrameResourceBuffers& lhs) = delete;
  ~FrameResourceBuffers() = default;

  // 加入一个项目，返回索引
  int Add(ID3D12Device* device, size_t count) {
    int index = static_cast<int>(items_.size());
    auto item = std::make_unique<UploadBuffer<T>>(device, count, is_constant_);
    items_.push_back(std::move(item));
    return index;
  }

  // 得到Buffer
  UploadBuffer<T>* Get(size_t index) { return items_[index].get(); }

private:
  // 父对象
  FrameResource* owner_;
  // 此Buffer是否是常量缓冲区（如果为false，一般是做为结构化缓冲区使用）
  bool is_constant_;
  // 对象的列表
  eastl::vector<UploadBufferPtr<T>> items_;
};

} // namespace ifire::dx

// namespace ifire::dx
